package com.link.play.fit.ai.ui.adjust.adjustPage.fragment

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.aigestudio.wheelpicker.WheelPicker.OnWheelChangeListener
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseFragment
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AdjustPageFragment2Binding
import com.link.play.fit.ai.ui.adjust.adjustPage.AdjustPageModel
import com.link.play.fit.ai.utils.HealthUtils.cmToFeetInch
import com.link.play.fit.ai.utils.HealthUtils.convertKgToLbs
import com.link.play.fit.ai.utils.HealthUtils.convertLbsToKg
import com.link.play.fit.ai.utils.HealthUtils.feetInchToCm


/**
 *Create by SleepDog on 2025-03-24
 */
class AdjustPageFragment2 :
    BaseFragment<AdjustPageFragment2Binding>(AdjustPageFragment2Binding::inflate) {
    private val viewModel: AdjustPageModel by lazy {
        ViewModelProvider(requireActivity())[AdjustPageModel::class.java]
    }

    //    private var recentlyGoalData: RecentlyGoalData? = null
    private var userGuide: UserGuide? = null

    private var mCurrentFt = 0
    private var mCurrentIn = 0
    private var mCurrentLb = 0
    private var mCurrentCm = 0
    private var mCurrentKg = 0

    private val ftData by lazy {
        val list = mutableListOf<String>()
        var start = 1 - 1
        repeat(8 - start) {
            start++
            list.add("$start ft")
        }
        list.toList()
    }
    private val inData by lazy {
        val list = mutableListOf<String>()
        var start = 0 - 1
        repeat(11 - start) {
            start++
            list.add("$start in")
        }
        list.toList()
    }
    private val lbData by lazy {
        val list = mutableListOf<String>()
        var start = 0 - 1
        repeat(800 - start) {
            start++
            list.add("$start lb")
        }
        list.toList()
    }
    private val cmData by lazy {
        val list = mutableListOf<String>()
        var start = 60 - 1
        repeat(243 - start) {
            start++
            list.add("$start cm")
        }
        list.toList()
    }
    private val kgData by lazy {
        val list = mutableListOf<String>()
        var start = 20 - 1
        repeat(360 - start) {
            start++
            list.add("$start kg")
        }
        list.toList()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initListener()
    }

    private fun initData() {
        userGuide = viewModel.getGoalData()
        with(binding) {
            ftWheelPicker.data = ftData
            inWheelPicker.data = inData
            lbWheelPicker.data = lbData
            cmWheelPicker.data = cmData
            kgWheelPicker.data = kgData
            userGuide?.let {
                ftWheelPicker.setSelectedItemPosition(
                    ftData.indexOf("${it.heightFt} ft"),
                    false
                )
                inWheelPicker.setSelectedItemPosition(
                    inData.indexOf("${it.heightIn} in"),
                    false
                )
                lbWheelPicker.setSelectedItemPosition(
                    lbData.indexOf("${it.weightLbs} lb"),
                    false
                )
                cmWheelPicker.setSelectedItemPosition(
                    cmData.indexOf("${it.heightCm} cm"),
                    false
                )

                kgWheelPicker.setSelectedItemPosition(
                    kgData.indexOf("${it.weightKg} kg"),
                    false
                )
                if (it.imperialMetric) {
                    binding.switchButton.isChecked = true
                    viewModel.setGoalsValue(isCm = true)
                    binding.imperialLayout.isVisible = false
                    binding.metricLayout.isVisible = true
                } else {
                    binding.switchButton.isChecked = false
                    viewModel.setGoalsValue(isCm = false)
                    binding.imperialLayout.isVisible = true
                    binding.metricLayout.isVisible = false
                }
            }

            mCurrentFt = ftData[ftWheelPicker.currentItemPosition].split(" ")[0].toInt()
            mCurrentIn = inData[inWheelPicker.currentItemPosition].split(" ")[0].toInt()
            mCurrentLb = lbData[lbWheelPicker.currentItemPosition].split(" ")[0].toInt()
            mCurrentCm = cmData[cmWheelPicker.currentItemPosition].split(" ")[0].toInt()
            mCurrentKg = kgData[kgWheelPicker.currentItemPosition].split(" ")[0].toInt()

        }
    }

    private fun initListener() {
        binding.switchButton.setOnCheckedChangeListener { _, isChecked ->
            runCatching {
                if (isChecked) {
                    binding.imperialLayout.isVisible = false
                    binding.metricLayout.isVisible = true
                    activity?.let { binding.ImperialText.setTextColor(it.getColor(R.color.color_69)) }
                    activity?.let { binding.MetricText.setTextColor(it.getColor(R.color.black_11)) }
                    viewModel.setGoalsValue(isCm = true)
                    mCurrentCm = feetInchToCm(mCurrentFt, mCurrentIn)
                    binding.cmWheelPicker.setSelectedItemPosition(
                        cmData.indexOf("$mCurrentCm cm"),
                        false
                    )
                    mCurrentKg = convertLbsToKg(mCurrentLb)
                    binding.kgWheelPicker.setSelectedItemPosition(
                        kgData.indexOf("$mCurrentKg kg"),
                        false
                    )
                } else {
                    binding.imperialLayout.isVisible = true
                    binding.metricLayout.isVisible = false
                    activity?.let { binding.ImperialText.setTextColor(it.getColor(R.color.black_11)) }
                    activity?.let { binding.MetricText.setTextColor(it.getColor(R.color.color_69)) }
                    viewModel.setGoalsValue(isCm = false)
                    val (ft, inches) = cmToFeetInch(mCurrentCm)
                    mCurrentFt = ft
                    mCurrentIn = inches
                    binding.ftWheelPicker.setSelectedItemPosition(ftData.indexOf("$ft ft"), false)
                    binding.inWheelPicker.setSelectedItemPosition(
                        inData.indexOf("$inches in"),
                        false
                    )
                    mCurrentLb = convertKgToLbs(mCurrentKg)
                    binding.lbWheelPicker.setSelectedItemPosition(
                        lbData.indexOf("$mCurrentLb lb"),
                        false
                    )
                }
            }

        }
        binding.ftWheelPicker.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                runCatching {
                    val cmValue = ftData[position].split(" ")[0].toInt()
                    mCurrentFt = cmValue
                    mCurrentCm = feetInchToCm(mCurrentFt, mCurrentIn)
                    viewModel.setGoalsValue(ft = cmValue, heigh = mCurrentCm)
                }
            }
        })
        binding.inWheelPicker.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                runCatching {
                    val inValue = inData[position].split(" ")[0].toInt()
                    mCurrentIn = inValue
                    mCurrentCm = feetInchToCm(mCurrentFt, mCurrentIn)
                    viewModel.setGoalsValue(inches = inValue, heigh = mCurrentCm)

                }
            }
        })
        binding.lbWheelPicker.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                runCatching {
                    val lbValue = lbData[position].split(" ")[0].toInt()
                    mCurrentLb = lbValue
                    mCurrentKg = convertLbsToKg(mCurrentLb)
                    viewModel.setGoalsValue(lbs = lbValue, weightKg = mCurrentKg)
                }
            }
        })
        binding.cmWheelPicker.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                runCatching {
                    val cmValue = cmData[position].split(" ")[0].toInt()
                    mCurrentCm = cmValue
                    val (ft, inches) = cmToFeetInch(mCurrentCm)
                    mCurrentFt = ft
                    mCurrentIn = inches
                    viewModel.setGoalsValue(heigh = cmValue, ft = ft, inches = inches)
                }

            }
        })
        binding.kgWheelPicker.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                runCatching {
                    val kgValue = kgData[position].split(" ")[0].toInt()
                    mCurrentKg = kgValue
                    mCurrentLb = convertKgToLbs(mCurrentKg)
                    viewModel.setGoalsValue(weightKg = kgValue, lbs = mCurrentLb)

                }
            }
        })
    }

}